﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Caifu.Fx;
using Caifu.Clients;
using Itfort;
namespace Caifu.WebSite
{
    public partial class GeneralLogin : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.HttpMethod.Equals("POST"))
            {
                //提交表单的次数
                PostCount++;


            }
            if (PostCount > MAXREQUEST)
            { 
                //这里要显示验证码
            }
        }

        /// <summary>
        /// 验证表单；检测用户的输入是否合法
        /// </summary>
        /// <param name="verifyCode">验证码</param>
        /// <param name="mail">用户邮箱</param>
        /// <param name="password">用户密码</param>
        /// <returns></returns>
        Boolean VerifyForm(String verifyCode, String mail, String password)
        {
            if (PostCount > MAXREQUEST + 1)
            {
                if (!(new ValidateCodeManager(HttpContext.Current))
                           .ValidationCode(verifyCode, "log"))
                {
                    ShowMessage("验证码错误或过期");
                    return false;
                }
            }
            if (String.IsNullOrEmpty(mail) || String.IsNullOrEmpty(password))
            {
                ShowMessage("用户名或密码错误");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="mail">用户邮箱地址</param>
        /// <param name="password">用户密码</param>
        /// <param name="isRemember">是否记住密码</param>
        void Login(String mail, String password, Boolean isRemember = false)
        {
            var user = UserClient.Instance.Login(mail, MD5Hash.ComputeHash(password));
            if (user != null)
            {
                var ticket = Comb.NewComb();
                if (SsoClient.Instance.GetLoginInfoByUserId(user.Id) != null)
                {
                    SsoClient.Instance.UpdateSsoInfo(user.Id, ticket);
                }
                else
                {
                    SsoClient.Instance.AddSsoInfo(user.Id, ticket);
                }
                AddCookie(ticket.ToString("N"), isRemember ? new TimeSpan(30, 0, 0, 0, 0) : new TimeSpan(0));
            }
            else
            {
                ShowMessage("用户名或密码错误");
            }
        }


        /// <summary>
        /// 添加cookie
        /// </summary>
        /// <param name="ticket">临时票据</param>
        /// <param name="expire">保存时间</param>
        void AddCookie(String ticket, TimeSpan expire)
        {
            HttpCookie cookie = new HttpCookie("u", ticket);
            cookie.Domain = "caifu520.com";
            cookie.Path = "/";
            cookie.Expires = DateTime.Now + expire;
            Response.Cookies.Add(cookie);
        }
    }
}